package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbAddress;
import com.dongdongshop.pojo.TbProvinces;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.vo.AddressVO;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    @DubboReference
    private AddressService addressService;

    //跳转地址管理页面
    @RequestMapping("toHomeSettingAddress")
    public String toHomeIndex(){
        return "home-setting-address";
    }

    //查询地址表
    @RequestMapping("getAddressInfo")
    @ResponseBody
    public AjaxResult getAddressInfo(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        List<AddressVO> addressList = addressService.getAddressInfo(user.getId());
        return AjaxResult.OK().setData(addressList);
    }

    //添加地址
    @RequestMapping("addAddress")
    @ResponseBody
    public AjaxResult addAddress(TbAddress address){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            address.setUserId(user.getUsername());
            int i  = addressService.addAddress(address);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //地址回显
    @RequestMapping("echoAddress")
    @ResponseBody
    public AjaxResult echoAddress(Long id){
        try {
            TbAddress address = addressService.echoAddress(id);
            return AjaxResult.OK().setData(address);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //地址修改
    @RequestMapping("updateAddress")
    @ResponseBody
    public AjaxResult updateAddress(TbAddress address){
        try {
            int i  = addressService.updateAddress(address);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除
    @RequestMapping("deleteId")
    @ResponseBody
    public AjaxResult deleteId(Long id){
        try {
            int i  = addressService.deleteId(id);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改默认地址
    @RequestMapping("updateAddByIsDefault")
    @ResponseBody
    public AjaxResult updateAddByIsDefault(TbAddress address){
        try {
            address.setIsDefault("1");
            int i  = addressService.updateAddress(address);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
